﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace KINLO.MES
{
    public static class WebAPIClass
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WebAPIClass));
        public static string HttpPost(string url, string body)
        {
            try
            {
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ReadWriteTimeout = 30000;
                request.Timeout = 30000;
                request.Method = "POST";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
                request.ServicePoint.Expect100Continue = false;
                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                var msg = string.Format("地址:" + url + "错误:" + ex.ToString());
                log.Error(msg);
                return null;
            }
            catch (Exception ex)
            {
                var msg = string.Format("地址:" + url + "错误:" + ex.ToString());
                log.Error(msg);
                return null;
            }
        }

        static HttpClient http2 = new HttpClient();//重传连接

        static HttpClient http = new HttpClient();//实时上传连接
        public static async Task<string> UpLoad_Mes(string url, string body, int httpClientIndex = 1)
        {
            try
            {
                var httpclient = httpClientIndex == 1 ? http : http2;
                var uploadJson = new StringContent(body);
                uploadJson.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                try
                {
                    var rsp = await httpclient.PostAsync(url, uploadJson);
                    rsp.EnsureSuccessStatusCode();
                    var rspStr = await rsp.Content.ReadAsStringAsync();
                    return rspStr;
                }
                catch (AggregateException aex)
                {
                    var msg = string.Format("地址:" + url + "错误:" + aex.ToString());
                    log.Error(msg);
                    return null;
                }
                catch (Exception ex)
                {
                    var msg = string.Format("地址:" + url + "错误:" + ex.ToString());
                    log.Error(msg);
                    return null;
                }
            }
            catch (Exception ex)
            {
                var msg = string.Format("地址:" + url + "错误:" + ex.ToString());
                log.Error(msg);
                return null;
            }
        }
    }
}
